/*   
 * Copyright 2009-2010 Rob Schoening
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.beaconframework.vaadin.ui;

import java.net.URL;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beaconframework.util.ThreadContext;
import org.beaconframework.vaadin.util.VaadinContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.Application;
import com.vaadin.service.ApplicationContext;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;

public class GenericApplication extends Application implements
        HttpServletRequestListener {

    Logger log = LoggerFactory.getLogger(GenericApplication.class);
    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;

    public GenericApplication() {

        super();

        ThreadContext.get().put(Application.class, this);
        setUser(org.springframework.security.core.context.SecurityContextHolder
                .getContext().getAuthentication().getPrincipal());
    }

    @Override
    public synchronized void init() {

    }

    public void onRequestEnd(HttpServletRequest request,
            HttpServletResponse response) {

    }

    public void onRequestStart(HttpServletRequest request,
            HttpServletResponse response) {

        ThreadContext.get().put(Application.class, this);

    }

    @Override
    public void start(URL applicationUrl, Properties applicationProperties,
            ApplicationContext context) {

        log.info("{}.start()", this);
        ThreadContext.get().put(Application.class, this);
        super.start(applicationUrl, applicationProperties, context);

        FeatureInjector.inject(VaadinContext.getApplicationContext());

    }

}
